/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.node;

import im.general.AbstractListenOnceBehaviour;
import im.general.Config;
import jade.core.behaviours.SimpleBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gnaskky
 */
public class ClusterShrinkingListenerBehaviour extends AbstractListenOnceBehaviour {

    MobileNodeAgent mn;

    public ClusterShrinkingListenerBehaviour(MobileNodeAgent mn) throws Exception {
        this.mn = mn;
        ACLMessage wrap_msg = new ACLMessage( ACLMessage.CFP );
        wrap_msg.addUserDefinedParameter( Config.IM_MSG_TYPE, Config.SHRINK_NOTIFY_MSG);
        mt = MessageTemplate.MatchCustom(wrap_msg, true);
    }

    public void handleMessage( ACLMessage in_msg ) {
        try {
           if( Config.TRACE_LVL > Config.ULTRA_TR_LVL )
                System.out.println("[ClusterShrinkingLis]\tSHRINK_NOTIFY_MSG [MN CH]\t"
                        + mn.getLocalName() + "\t" + mn.getClusterHeadID() );

            // got ch list
            mn.setCHs((Hashtable<String,Double>) in_msg.getContentObject());

            // unvalidate old CH
            mn.setClusterHeadID(null);

            //this flag is only used for tracing purposes
            mn.isShrinkJoin = true;
            this.mn.resetShrinkChHnld();
            // go next behaviour
            this.setDone();

        } catch (Exception ex) {
            Logger.getLogger(NewCHNotifyListenerBehaviour.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }

}


/*
 *
public class ClusterShrinkingListenerBehaviour extends SimpleBehaviour {

    MobileNodeAgent mn;
    MessageTemplate mt;
    boolean finished = false;



    public ClusterShrinkingListenerBehaviour(MobileNodeAgent mn) throws Exception {
        this.mn = mn;
        ACLMessage wrap_msg = new ACLMessage( ACLMessage.INFORM );
        wrap_msg.addUserDefinedParameter( Config.IM_MSG_TYPE, Config.SHRINK_NOTIFY_MSG);
        mt = MessageTemplate.MatchCustom(wrap_msg, true);
    }

    public void action() {
        try {
            ACLMessage in_msg = null;

            in_msg = mn.receive(mt);

            if(in_msg != null) {
                if( Config.TRACE_LVL > Config.ULTRA_TR_LVL )
                    System.out.println("[ClusterShrinkingLis]\tSHRINK_NOTIFY_MSG [MN CH]:\t"
                            + mn.getName() + "\t" + mn.getClusterHeadID() );

                // got ch list
                mn.setCHs((Vector<String>) in_msg.getContentObject());
                // unvalidate old CH
                mn.setClusterHeadID(null);

                / * go next behaviour * /
                finished = true;
            } else
                block();

        } catch (Exception ex) {
            Logger.getLogger(NewCHNotifyListenerBehaviour.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }

    }

    @Override
    public boolean done() {
        return finished;
    }

}
 * */